#import "RNDetailHeaderCell.h"
#import "NSCell+SRAdditions.h"
#import "NSShadow+MCAdditions.h"

static NSUInteger const detailHeaderCellTitleLeftPadding = 20;
static NSUInteger const detailHeaderCellTitleBottomPadding = 4;

@implementation RNDetailHeaderCell

@synthesize mouseIsHovering = mouseIsHovering_;
@synthesize titleAttributesNormal = titleAttributesNormal_;
@synthesize titleAttributesHover = titleAttributesHover_;
//@synthesize trackingMouseDown = trackingMouseDown_;

+ (BOOL)prefersTrackingUntilMouseUp {
    return YES;
}

- (id)copyWithZone:(NSZone *)zone {
    RNDetailHeaderCell *copy = [super copyWithZone:zone];
    if (!copy) 
        return nil;
    [copy setMouseIsHovering:[self mouseIsHovering]];
    return copy;
}

-(BOOL)isEditable {
    return NO;
}

-(void)setObjectValue:(id)obj {
    if (obj && ![obj isKindOfClass:[NSDictionary class]])
        return;
    [super setObjectValue:[obj valueForKey:@"title"]];
}

-(NSDictionary *)titleAttributesNormal {
    if (!titleAttributesNormal_) {
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setValue:[NSFont systemFontOfSize:11.0f] forKey:NSFontAttributeName];
        [attributes setValue:[NSColor colorWithCalibratedWhite:0.4 alpha:1.0] forKey:NSForegroundColorAttributeName];
        [attributes setValue:[[NSShadow alloc] initWithColor:[NSColor colorWithCalibratedWhite:0.9 alpha:1.0] offset:NSMakeSize(0, -1) blurRadius:1] forKey:NSShadowAttributeName];
        titleAttributesNormal_ = (NSDictionary *)attributes;
    }
    return titleAttributesNormal_;
}

-(NSDictionary *)titleAttributesHover {
    if (!titleAttributesHover_) {
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setValue:[NSFont systemFontOfSize:11.0f] forKey:NSFontAttributeName];
        [attributes setValue:[NSColor colorWithCalibratedWhite:0.4 alpha:1.0] forKey:NSForegroundColorAttributeName];
        [attributes setValue:[[NSShadow alloc] initWithColor:[NSColor colorWithCalibratedWhite:0.9 alpha:1.0] offset:NSMakeSize(0, -1) blurRadius:1] forKey:NSShadowAttributeName];
        [attributes setValue:[NSNumber numberWithInteger:NSUnderlineStyleSingle | NSUnderlinePatternSolid] forKey:NSUnderlineStyleAttributeName];
        titleAttributesHover_ = (NSDictionary *)attributes;
    }
    return titleAttributesHover_;
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    [[self attributedStringValue] drawInRect:[self titleFrameForCellFrame:cellFrame]];
}

- (void)addTrackingAreasForView:(NSView *)controlView inRect:(NSRect)cellFrame withUserInfo:(NSDictionary *)userInfo mouseLocation:(NSPoint)currentPoint {
    NSRect titleFrame = [self titleFrameForCellFrame:cellFrame];
    NSTrackingAreaOptions options = NSTrackingEnabledDuringMouseDrag | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways;
    NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:titleFrame options:options owner:controlView userInfo:userInfo];
    [controlView addTrackingArea:area];
}

- (void)mouseEntered:(NSEvent *)event {
    [self setMouseIsHovering:YES];
    [(NSControl *)[self controlView] updateCell:self];
}

- (void)mouseExited:(NSEvent *)event {
    [self setMouseIsHovering:NO];
    [(NSControl *)[self controlView] updateCell:self];
}

-(NSRect)titleFrameForCellFrame:(NSRect)cellFrame {
    NSSize titleSize = [[self attributedStringValue] size];
    return NSMakeRect(detailHeaderCellTitleLeftPadding, NSMaxY(cellFrame) - titleSize.height - detailHeaderCellTitleBottomPadding, titleSize.width, titleSize.height);
}

- (NSAttributedString *)attributedStringValue {
    return [[NSAttributedString alloc] initWithString:[self stringValue] attributes:([self mouseIsHovering] ? [self titleAttributesHover] : [self titleAttributesNormal])];
}

- (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView {
    NSPoint point = [controlView convertPoint:[event locationInWindow] fromView:nil];
    if ([controlView mouse:point inRect:[self titleFrameForCellFrame:cellFrame]]) {
        return NSCellHitContentArea | NSCellHitTrackableArea;
    }
    return [super hitTestForEvent:event inRect:cellFrame ofView:controlView];
}

- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp {
//    [self setTrackingMouseDown:YES];
    NSRect titleRect = [self titleFrameForCellFrame:cellFrame];
    while ([theEvent type] != NSLeftMouseUp) {
        NSPoint eventPoint = [controlView convertPoint:[theEvent locationInWindow] fromView:nil];
        [self setMouseIsHovering:[controlView mouse:eventPoint inRect:titleRect]];
        if ([theEvent type] == NSMouseEntered || [theEvent type] == NSMouseExited) 
            [NSApp sendEvent:theEvent];
        theEvent = [[controlView window] nextEventMatchingMask:(NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSMouseEnteredMask | NSMouseExitedMask)];
    }

    if ([self mouseIsHovering]) {
        [NSApp sendAction:[self action] to:[self target] from:self];
        [(NSControl *)[self controlView] updateCell:self];
    }
//    [self setTrackingMouseDown:NO];
    return YES;
}

- (NSRect)expansionFrameWithFrame:(NSRect)cellFrame inView:(NSView *)view {
    return NSZeroRect;
}

@end
